package com.company;

public class C11ErFenChaZhaoFa {
    public static void main(String args[]){
//        有序数组
        int[] arrayData= new int[]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

        int compareData=13;
        System.out.println(getIndex(arrayData,compareData));
         compareData=0;
        System.out.println(getIndex(arrayData,compareData));
        compareData=6;
        System.out.println(getIndex(arrayData,compareData));
    }
    /*
    * 二分查找法
    * @param arrayData 有序数组
    * @param compareData 比较数据
    * @return int 返回下标
    * */
    public static int getIndex(int[] arrayData ,int compareData){
        int height = arrayData.length-1;
        int low =0;
        while(low<=height){
            int mid= low+(height-low)/2;
            if (arrayData[mid]>compareData){
                height=mid-1;
            }else if (arrayData[mid]==compareData){
                return mid;
            }else{
                low=mid+1;
            }
        }
        return -1;

    }
}
